#include "queue.h"

int Init(Queue *queue)
{
     queue->base = malloc(sizeof(datatype) * MAXSIZE);
     if (queue->base == NULL)
          return -1;
     queue->front = queue->rear = 0;
     return 0;
}

int EnQueue(Queue *queue, datatype data)
{
     if ((queue->rear + 1) % MAXSIZE == queue->front)
          return -1;
     queue->base[queue->rear++] = data;
     queue->rear %= MAXSIZE;
     return 0;
}

int DeQueue(Queue *queue)
{
     if (queue->front == queue->rear)
          return -1;
     queue->front = (queue->front + 1) % MAXSIZE;
     return 0;
}

void Show(Queue *queue)
{
     for (int i = queue->front; i != queue->rear;)
     {
          printf("%d  ", queue->base[i]);
          i = (i + 1) % MAXSIZE;
     }
     printf("\n");
}

int Length(Queue *queue)
{
     int i = queue->rear - queue->front;
     return i > 0 ? i : i + MAXSIZE;
}

void GetHead(Queue *queue, datatype *value)
{
     if (queue->front == queue->rear)
          return;
     *value = queue->base[queue->front];
}

void Destory(Queue *queue)
{
     free(queue->base);
     queue->base = NULL;
}
